//
//  materialDataBase.m
//  NewProject
//
//  Created by 李赐岩 姜雪 张昕锐 on 15/3/9.
//  Copyright (c) 2015年 李赐岩 姜雪 张昕锐. All rights reserved.
//

#import "materialDataBase.h"

@implementation materialDataBase
+(instancetype)shareClassifyDataBaseHandle
{
    static materialDataBase *materialDBH = nil;
    
    if (materialDBH == nil) {
        materialDBH = [[materialDataBase alloc] init];
    }
    return materialDBH;
}

static sqlite3 *marterialDB;


-(void)openDB
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentFielPath = [array lastObject];
    
    NSString *sqlitePath = [documentFielPath stringByAppendingPathComponent:@"class.sqlite"];
    
    NSLog(@"99999999 %@", sqlitePath);
    if (marterialDB != nil) {
        return;
    }
    
    int result = sqlite3_open(sqlitePath.UTF8String, &marterialDB);
    
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else
    {
        NSLog(@"数据库打开失败");
    }
}

// 创建表
-(void)createTable
{
    
    NSString *createSql = @"CREATE TABLE IF NOT EXISTS materialClass(number INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT,  url TEXT, thumb TEXT)";
    
    // 执行sql语句
    int result = sqlite3_exec(marterialDB, createSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
    }
    
}

// 插入数据
-(void)insertMaterial:(MaterialModel *)material
{
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO materialClass(name, url, thumb)  VALUES('%@', '%@', '%@')", material.name, material.url, material.thumb];
    
    //  执行sql语句
    int result = sqlite3_exec(marterialDB, insertSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
    
}

// 删除一个数据
-(void)deleteClassify:(NSInteger)number
{
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM materialClass WHERE number = '%ld'", (long)number];
    
    int result = sqlite3_exec(marterialDB, deleteSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
        NSLog(@"删除失败");
    }
    
}

// 查询所有数据
-(NSMutableArray *)selectClassify
{
    NSString *selectSql = @"SELECT * FROM materialClass";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(marterialDB, selectSql.UTF8String, -1, &stmt, nil);
    
    NSMutableArray *array = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            int number = sqlite3_column_int(stmt, 0);
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *url = sqlite3_column_text(stmt, 2);
            const unsigned char *thumb = sqlite3_column_text(stmt, 3);
            
            NSInteger numberInteger = number;
            NSString *nameStr = [NSString stringWithUTF8String:(const char *)name];
            NSString *urlStr = [NSString stringWithUTF8String:(const char *)url];
            NSString *thumbStr = [NSString stringWithUTF8String:(const char *)thumb];
            

            MaterialModel *material = [[MaterialModel alloc] init];
            
            material.name = nameStr;
            material.url = urlStr;
            material.thumb = thumbStr;
            material.number = numberInteger;
            
            [array addObject:material];
        }
        
    }else
    {
        NSLog(@"查询准备失败");
    }
    
    return array;
    
}
-(void)deleteAllObject
{
    NSString *deleteBase = @"DELETE * FROM materialClass";
    
    
    int result = sqlite3_exec(marterialDB, deleteBase.UTF8String, NULL, NULL, nil);

    if (result == SQLITE_OK) {
        NSLog(@"delete success");
    }else{
        NSLog(@"fail");
    }
    
    
}

-(void)deleteTable
{
    NSString *deleteTabel = @"DROP TABLE materialClass";
    
    int result = sqlite3_exec(marterialDB, deleteTabel.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"成功");
    }else
    {
        NSLog(@"失败");
    }
    
    
}

@end
